package com.gperp.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gperp.common.GPResult;
import com.gperp.mapper.AsRoleMapper;
import com.gperp.pojo.AsRole;
import com.gperp.pojo.AsRoleExample;
import com.gperp.service.RoleService;

@Service
public class RoleServiceImpl implements RoleService {

	@Resource
	private AsRoleMapper asRoleMapper;
	
	@Override
	public List<AsRole> queryList() {
		return asRoleMapper.selectByExample(new AsRoleExample());
	}
	
	@Override
	public GPResult checkName(String rolename) {
		AsRoleExample example = new AsRoleExample();
		example.createCriteria().andRolenameEqualTo(rolename);
		List<AsRole> role = asRoleMapper.selectByExample(example);
		if(role!=null&&role.size()>0){
			return GPResult.build(200, "NO");
		}
		return GPResult.ok();
	}

	@Override
	public AsRole queryRoleById(Long id) {
		return asRoleMapper.selectByPrimaryKey(id);
	}

	@Override
	public GPResult saveRole(AsRole role) {
		try {
			role.setCreatedby("admin");
			role.setCreationtime(new Date());
			asRoleMapper.insertSelective(role);
		} catch (Exception e) {
			e.printStackTrace();
			return GPResult.build(200, "NO");
		}
		return GPResult.ok();
	}

	@Override
	public GPResult updateRole(AsRole role) {
		try {
			asRoleMapper.updateByPrimaryKeySelective(role);
		} catch (Exception e) {
			e.printStackTrace();
			return GPResult.build(200, "NO");
		}
		return GPResult.ok();
	}

	@Override
	public GPResult deleteRoleById(Long id) {
		try {
			asRoleMapper.deleteByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			return GPResult.build(200, "NO");
		}
		return GPResult.ok();
	}
	@Override
	public List<AsRole> queryListStart() {
		AsRoleExample expmple = new AsRoleExample();
		expmple.createCriteria().andIsstartEqualTo(1);
		return asRoleMapper.selectByExample(expmple);
	}
}
